home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / iau.arc / TYPE.C < prev    next >
C/C++ Source or Header  |  1986-10-25  |  4KB  |  166 lines

  1. #include <stdio.h>
  2. #include <hamdefs.h>
  3. #include <ctype.h>
  4.  
  5. char *index(),*rindex(),*calloc();
  6. char line[132];
  7. char **lines;
  8. int linecount,width,height;
  9. FILE *ptr, *fopen();
  10.  
  11. type(filename,top,left,bottom,right)
  12. char *filename;
  13. int top,left,bottom,right;
  14. {
  15. /*  this routine displays a file in the lower window of the
  16.     screen, and allows the user to use the cursor keys to
  17.     view more and more of the file.  standard text files
  18.     are used, which can also be printed and used by other
  19.     DOS functions
  20. */
  21.     int cur_top,i,j;
  22.  
  23.     lines=calloc(100,sizeof(char *));
  24.     csrpush();
  25.     width=right-left-1;
  26.     height=bottom-top-1;
  27.     linecount=0;
  28.     
  29.     clrblk(top,left,bottom,right);
  30.     scrbox(top,left,bottom,right,3,NORMAL|HILITE);
  31.     atputsha(top,left+(width/2)-6,"Press \030 or \031");
  32.     atputsha(bottom,left+(width/2)-6,"Esc when done");
  33.  
  34.     ptr=fopen(filename,"r");
  35.     if(ptr==NULL) {
  36.     clrblk(top,left,bottom,right);
  37.     csrpop();
  38.     free(lines);
  39.     return -1;
  40.     }
  41.  
  42.     fillarray();
  43.  
  44.     cursor(FALSE);    
  45.     for(i=0;i<linecount && i<height; i++)
  46.     atputsha(top+1+i,left+1,lines[i]);
  47.  
  48.     cur_top=0;
  49.  
  50.     for(;;) {
  51.     switch(j=inkeyi()) {
  52.         case HOME:    cur_top=0;
  53.             clrblk(top+1,left+1,bottom-1,right-1);
  54.             for(i=0;i<linecount && i<height; i++)
  55.                 atputsha(top+1+i,left+1,lines[i]);
  56.             break;
  57.         
  58.         case END:    cur_top=linecount-height;
  59.             clrblk(top+1,left+1,bottom-1,right-1);
  60.             for(i=0;i<linecount && i<height; i++)
  61.                 atputsha(top+1+i,left+1,lines[i+cur_top]);
  62.             break;
  63.         
  64.         case PAGEUP:
  65.             if(cur_top==0)
  66.                 break;
  67.             cur_top-=height;
  68.             if(cur_top<0)
  69.                 cur_top=0;
  70.             clrblk(top+1,left+1,bottom-1,right-1);
  71.             for(i=0;i<linecount && i<height; i++)
  72.                 atputsha(top+1+i,left+1,lines[i+cur_top]);
  73.             break;
  74.         
  75.         case PAGEDN:
  76.             if(cur_top==linecount-height)
  77.                 break;
  78.             cur_top+=height;
  79.             if(cur_top>(linecount-height))
  80.                 cur_top=linecount-height;
  81.             clrblk(top+1,left+1,bottom-1,right-1);
  82.             for(i=0;i<linecount && i<height; i++)
  83.                 atputsha(top+1+i,left+1,lines[i+cur_top]);
  84.             break;
  85.         
  86.         case DOWN:    if(cur_top+height < linecount) {
  87.                 ++cur_top;
  88.                 scroll(1,top+1,left+1,bottom-1,right-1);
  89.                 atputsha(bottom-1,left+1,lines[cur_top+height-1]);
  90.             }
  91.             break;
  92.         case UP:    if(cur_top>0) {
  93.                 --cur_top;
  94.                 scroll(-1,top+1,left+1,bottom-1,right-1);
  95.                 atputsha(top+1,left+1,lines[cur_top]);
  96.             }
  97.             break;
  98.         case ESC:    clrblk(top,left,bottom,right);
  99.             cursor(TRUE);
  100.             csrpop();
  101.             for(i=0;i<linecount;i++)
  102.                 free(lines[i]);
  103.             free(lines);
  104.             return 0;
  105.             break;
  106.         default:    break;
  107.     }
  108.     }
  109. }
  110.  
  111. cursor(select)
  112. int select;
  113. {
  114.     if(select)
  115.     v_scsrtp(12,13);
  116.     else
  117.     v_scsrtp(15,15);
  118. }
  119.  
  120. fillarray()
  121. {
  122.     int length,i,j;
  123.     char *leftcp,*rightcp;
  124.  
  125.     lines[linecount]=calloc(width+1,sizeof(char));
  126.     while(!feof(ptr)) {
  127.     if(!fgets(line,sizeof(line),ptr))
  128.         break;
  129.     leftcp=rightcp=&line[0];
  130.     *index(line,'\n')='\0';
  131.     if(!strlen(line)) {
  132.         linecount+=2;
  133.         lines[linecount-1]=calloc(1,1);
  134.         lines[linecount]=calloc(width+1,sizeof(char));        
  135.         continue;
  136.     }
  137.  
  138.     /* both ptrs at first letter */
  139.  
  140.     while(*rightcp) {
  141.         while(!(isspace(*rightcp)) && (*rightcp!='\0'))
  142.         ++rightcp;
  143.         length=rightcp-leftcp;
  144.         if(strlen(lines[linecount])+length < width) {
  145.         strncat(lines[linecount],leftcp,length);
  146.         strcat(lines[linecount]," ");
  147.         } else {
  148.         lines[++linecount]=calloc(width+1,sizeof(char));
  149.         strncat(lines[linecount],leftcp,length);
  150.         strcat(lines[linecount]," ");
  151.         }
  152.         leftcp=rightcp;
  153.         while(isspace(*leftcp) && *leftcp!='\0')
  154.         ++leftcp,++rightcp;
  155.     }
  156.     }
  157.     ++linecount;
  158. }
  159.  
  160. center(row,string)
  161. int row;
  162. char *string;
  163. {
  164.     atputsha(row,40-(strlen(string)/2),string);
  165. }
  166.